go to previous page   go to home page   go to next page
DecimalFormat numform = new DecimalFormat("000.0"); 
System.out.println( "Num = " + numform.format(1.19) );

Answer:

Num = 001.2

Thousands Separator

The format pattern on the left of the decimal separator can include commas to show thousands-separators. For example, this sequence

DecimalFormat numform = 
    new DecimalFormat("000,000.00"); 
    
System.out.println( "Num = " +  
    numform.format(12345.678) );

writes out

012,345.68

Even in locales where comma is not the proper thousands separator, the format pattern uses a comma. The output string will use the correct separator for the default locale. (There are methods that change this behavior, not covered here.)

Don't put thousands-separators in the characters on the right of the decimal point.

The groups of digits separated by the thousands separator must all be the same size (usually groups of three digits). It is OK if the first group has fewer digits.


QUESTION 9:

What does the following fragment write?

DecimalFormat numform = new DecimalFormat("000,000.00"); 
System.out.println( "Num = " + numform.format(98765.432) );